package com.peter.recyclerviewdemo;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class RecyclerViewDemo extends AppCompatActivity {

    private final static String TAG = RecyclerViewDemo.class.getSimpleName();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setupRecyclerViewList();
    }

    /**
     * add a item click listener
     */
    private View.OnClickListener mItemClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int position = (int) v.getTag();
            switch (position) {
                case 0:
                    // sync is disabled
                    Log.e(TAG, "onClick: 000000000000000", null);
                    break;
                case 1:
                    //sync is running
                    break;
                case 2:
                    //sync not start
                    break;
                case 3:
                    //sync finish
                    break;
                case 4:
                    break;
                default:
                    break;
            }
        }
    };

    /**
     * set up the recyclerview
     */
    private void setupRecyclerViewList() {
        mRecyclerView = (RecyclerView) findViewById(R.id.list_main);
        mLinearLayoutRecyclerViewAdapter.addData();
        mRecyclerView.setAdapter(mLinearLayoutRecyclerViewAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
        mRecyclerView.setLayoutManager(layoutManager);
    }

    RecyclerView mRecyclerView;

    /**
     * the adapter instance
     */
    private AnimationAdapter mLinearLayoutRecyclerViewAdapter = new AnimationAdapter();

    /**
     * the adapter of a recyclerview
     */
    public class AnimationAdapter extends RecyclerView.Adapter<ImageViewHolder> {
        private ArrayList<Integer> arrayList = new ArrayList<>();
        private int num;

        public void addData() {
            int count = 20/*(int) (Math.random() * 3) + 1*/;
            int index = 0/*(int) (Math.random() * arrayList.size())*/;
            for (int i = 0; i < count; i++) {
                arrayList.add(index, num++);
            }
            notifyItemRangeInserted(index, count);
        }

        public void deleteData() {
            if (!arrayList.isEmpty()) {
                int index = (int) (Math.random() * arrayList.size());
                arrayList.remove(index);
                notifyItemRemoved(index);
            }
        }

        public void changeData() {
            if (!arrayList.isEmpty()) {
                int index = (int) (Math.random() * arrayList.size());
                arrayList.set(index, num++);
                notifyItemChanged(index);
            }
        }

        @NonNull
        @Override
        public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            ImageViewHolder imageViewHolder = new ImageViewHolder(getLayoutInflater().inflate(R.layout.image, parent, false));
            return imageViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
            holder.bindImage(getResources().getDrawable(R.mipmap.ic_launcher));

            //set the bottom textview text ,index the data
            holder.setRawId(arrayList.get(position));

            // 通过tag定位item
            holder.itemView.setTag(position);
            // set the listener
            holder.itemView.setOnClickListener(mItemClickListener);

            holder.setText("");
            switch (position) {
                case 0:
                    holder.setText("测试同步未开启1");
                    break;
                case 1:
                    holder.setText("测试同步中");
                    break;
                case 2:
                    holder.setText("测试同步未开始");
                    break;
                case 3:
                    holder.setText("测试同步结束");
                    break;
                case 4:
                    holder.setText("测试Customview");
                    break;
                default:
                    break;
            }
        }

        @Override
        public int getItemCount() {
            return arrayList.size();
        }
    }
}
